<template>
  <ContentWrap>
    <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="150px" :disabled="disabled">
      <el-row>
        <el-col :span="12">
          <el-form-item label="微服务类型" prop="cloudType">
            <el-select v-model="formData.cloudType" placeholder="请选择微服务类型" @change="changeType" clearable>
              <el-option v-for="item in typeOption" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="交换机名称" prop="exchangeName">
            <el-input v-model="formData.exchangeName" clearable placeholder="请输入交换机名称" :disabled="isDisabledName" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="队列名称" prop="queueName">
            <el-input v-model="formData.queueName" clearable placeholder="请输入队列名称" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="formData.remark" clearable placeholder="请输入备注" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="启用" prop="enable">
            <el-switch v-model="formData.enable" :active-value="1" :inactive-value="0" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div style="text-align: center">
          <el-button :disabled="formLoading" type="primary" @click="submitForm" v-if="!disabled">确 定</el-button>
          <el-button @click="setClose()">取 消</el-button>
    </div>
  </ContentWrap>
</template>
<script lang="ts" setup>
import * as customServicesApi from '@/api/customServices/index'
import {DICT_TYPE, getDictOptions,getIntDictOptions } from '@/utils/dict'
defineOptions({ name: 'ServersForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: null,
  exchangeName: '',
  queueName: '',
  cloudType: '',
  remark:'',
  enable: 0,
})
const typeOption = ref([])
const formRules = reactive({
  cloudType: [{ required: true, message: '微服务类型不能为空', trigger: 'change' }],
  exchangeName: [{ required: true, message: '交换机名称不能为空', trigger: 'blur' }],
  queueName: [{ required: true, message: '队列名称不能为空', trigger: 'blur' }],
  enable: [{ required: true, message: '启用标识不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref
const disabled = ref(false) //查看禁用

const open = async (type, id) => {
  resetForm()
  disabled.value = (type == 'detail')
  formType.value = type
  typeOption.value = await getIntDictOptions(DICT_TYPE.CLOUDTYPE)
  // 修改时，设置数据
  if (id) {
    // 获得区域列表
    formLoading.value = true
    try {
      formData.value = await customServicesApi.getCustomServices(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success', 'setClose']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    if (formType.value == 'update') {
      await customServicesApi.saveCustomServices({ ...formData.value })
      message.success(t('common.updateSuccess'))
    } else {
      await customServicesApi.saveCustomServices({ ...formData.value })
      message.success(t('common.createSuccess'))
    }
    // 发送操作成功的事件
    emit('success')
    setClose()
  } finally {
    formLoading.value = false
  }
}
const setClose = () => {
  emit('setClose')
}

const isDisabledName = ref(false)
const getExchangeName = (value) => {
  const option = typeOption.value.find(option => option.value === value);
  return option ? option.cssClass : '';
};const changeType = (value)=>{
  if(value == 1 || value == 2 || value ==3 || value == 4){
    console.log('getExchangeName');
    
    formData.value.exchangeName = getExchangeName(value)
    isDisabledName.value = true
  }else{
    formData.value.exchangeName = ''
    isDisabledName.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: null,
    exchangeName: '',
    queueName: '',
    cloudType: '',
    remark:'',
    enable: 0,
  }
  formRef.value?.resetFields()
}
</script>
